<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon32.ico?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon16.ico?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="template,">










<meta name="description" content="USRefl用99行完成了C++的静态反射，集合了大量C++14,17,20的优秀特性，也有诸多元编程技巧值得学习。我打算以这个.h为模板，DFS式学习模板语法和新标准特性。这此围绕ElemList展开。">
<meta name="keywords" content="template">
<meta property="og:type" content="article">
<meta property="og:title" content="[Template] index_sequence, parameter pack, fold expression">
<meta property="og:url" content="http://htelepathh.gitee.io/2021/05/25/Template-index-sequence-parameter-pack-fold-expression/index.html">
<meta property="og:site_name" content="OUTPUT">
<meta property="og:description" content="USRefl用99行完成了C++的静态反射，集合了大量C++14,17,20的优秀特性，也有诸多元编程技巧值得学习。我打算以这个.h为模板，DFS式学习模板语法和新标准特性。这此围绕ElemList展开。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://htelepathh.gitee.io/2021/05/25/Template-index-sequence-parameter-pack-fold-expression/stdinvoke.jpg">
<meta property="og:updated_time" content="2021-05-31T08:33:53.747Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="[Template] index_sequence, parameter pack, fold expression">
<meta name="twitter:description" content="USRefl用99行完成了C++的静态反射，集合了大量C++14,17,20的优秀特性，也有诸多元编程技巧值得学习。我打算以这个.h为模板，DFS式学习模板语法和新标准特性。这此围绕ElemList展开。">
<meta name="twitter:image" content="http://htelepathh.gitee.io/2021/05/25/Template-index-sequence-parameter-pack-fold-expression/stdinvoke.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":true},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://htelepathh.gitee.io/2021/05/25/Template-index-sequence-parameter-pack-fold-expression/">





  <title>[Template] index_sequence, parameter pack, fold expression | OUTPUT</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">OUTPUT</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">HTelepathH's blog</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-message">
          <a href="/message" rel="section">
            
            留言
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://htelepathh.gitee.io/2021/05/25/Template-index-sequence-parameter-pack-fold-expression/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="HTelepathH">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/MOB.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="OUTPUT">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">[Template] index_sequence, parameter pack, fold expression</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2021-05-25T17:03:26+08:00">
                2021-05-25
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/C/" itemprop="url" rel="index">
                    <span itemprop="name">C++</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>USRefl用99行完成了C++的静态反射，集合了大量C++14,17,20的优秀特性，也有诸多元编程技巧值得学习。我打算以这个.h为模板，DFS式学习模板语法和新标准特性。这此围绕<code>ElemList</code>展开。<br><a id="more"></a></p>
<h2 id="std-index-sequence-amp-std-tuple"><a href="#std-index-sequence-amp-std-tuple" class="headerlink" title="std::index_sequence &amp; std::tuple"></a>std::index_sequence &amp; std::tuple</h2><p><code>make_index_sequence&lt;N&gt;()</code>由编译器内部在编译期间实现的0,1,2…N-1，并不是基于现有的元编程的逻辑。C++14<br><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span>&lt; <span class="class"><span class="keyword">class</span> <span class="title">T</span>, <span class="title">T</span>... <span class="title">Ints</span> &gt;</span></span><br><span class="line"><span class="class"><span class="title">class</span> <span class="title">integer_sequence</span>;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">size_t</span>... Ints&gt;</span><br><span class="line"><span class="keyword">using</span> index_sequence = <span class="built_in">std</span>::integer_sequence&lt;<span class="keyword">size_t</span>, Ints...&gt;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">size_t</span> ...N&gt;</span><br><span class="line"><span class="function"><span class="keyword">static</span> <span class="keyword">constexpr</span> <span class="keyword">auto</span> <span class="title">square_nums</span><span class="params">(<span class="keyword">size_t</span> index, <span class="built_in">std</span>::index_sequence&lt;N...&gt;)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">constexpr</span> <span class="keyword">auto</span> nums = <span class="built_in">std</span>::<span class="built_in">array</span>&#123;N * N ...&#125;;</span><br><span class="line">    <span class="keyword">return</span> nums[index];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">size_t</span> N&gt;</span><br><span class="line"><span class="function"><span class="keyword">constexpr</span> <span class="keyword">static</span> <span class="keyword">auto</span> <span class="title">const_nums</span><span class="params">(<span class="keyword">size_t</span> index)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> square_nums(index, <span class="built_in">std</span>::make_index_sequence&lt;N&gt;&#123;&#125;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">static_assert</span>(const_nums&lt;<span class="number">101</span>&gt;(<span class="number">100</span>) == <span class="number">100</span> * <span class="number">100</span>);</span><br></pre></td></tr></table></figure></p>
<p>index_sequence 的例子，tuple的遍历打印<br><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> Tuple, <span class="keyword">typename</span> Func, <span class="keyword">size_t</span> ...N&gt;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">func_call_tuple</span><span class="params">(<span class="keyword">const</span> Tuple&amp; t, Func&amp;&amp; func, <span class="built_in">std</span>::index_sequence&lt;N...&gt;)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="built_in">std</span>::<span class="built_in">initializer_list</span>&lt;<span class="keyword">int</span>&gt;&#123;(func(<span class="built_in">std</span>::get&lt;N&gt;(t)), <span class="number">0</span>)...&#125;;</span><br><span class="line">    <span class="comment">// (,..,)expression :  c=(a=1,b); -&gt; c = b;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> ...Args, <span class="keyword">typename</span> Func&gt;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">travel_tuple</span><span class="params">(<span class="keyword">const</span> <span class="built_in">std</span>::tuple&lt;Args...&gt;&amp; t, Func&amp;&amp; func)</span> </span>&#123;</span><br><span class="line">	func_call_tuple(t, <span class="built_in">std</span>::forward&lt;Func&gt;(func), <span class="built_in">std</span>::make_index_sequence&lt;<span class="keyword">sizeof</span>...(Args)&gt;&#123;&#125;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">auto</span> t = <span class="built_in">std</span>::make_tuple(<span class="number">1</span>, <span class="number">4.56</span>, <span class="string">"happen lee"</span>);</span><br><span class="line">travel_tuple(t, [](<span class="keyword">auto</span>&amp;&amp; item) &#123; <span class="comment">//auto in lambda params, C++14</span></span><br><span class="line">    <span class="built_in">std</span>::<span class="built_in">cout</span> &lt;&lt; item &lt;&lt; <span class="string">", "</span>;</span><br><span class="line">    &#125;);</span><br></pre></td></tr></table></figure></p>
<h3 id="std-apply-C-17"><a href="#std-apply-C-17" class="headerlink" title="std::apply (C++17)"></a>std::apply (C++17)</h3><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">F</span>, <span class="title">class</span> <span class="title">Tuple</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">constexpr</span> <span class="title">decltype</span>(<span class="title">auto</span>) <span class="title">apply</span>(<span class="title">F</span>&amp;&amp; <span class="title">f</span>, <span class="title">Tuple</span>&amp;&amp; <span class="title">t</span>);</span></span><br></pre></td></tr></table></figure>
<p>用<code>std::tuple</code>的内容作为参数<code>std::invoke</code>可调用对象f。其实<code>Tuple</code>不一定为<code>std::tuple</code>，可以是任何支持<code>std::get</code>和<code>std::tuple_size</code>的东西，比如<code>std::array</code>(C++11),<code>std::pair</code>(C++11)。<br><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="comment">//possible implementation</span></span><br><span class="line"><span class="keyword">namespace</span> detail &#123;</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">F</span>, <span class="title">class</span> <span class="title">Tuple</span>, <span class="title">std</span>:</span>:<span class="keyword">size_t</span>... I&gt;</span><br><span class="line"><span class="function"><span class="keyword">constexpr</span> <span class="title">decltype</span><span class="params">(<span class="keyword">auto</span>)</span> <span class="title">apply_impl</span><span class="params">(F&amp;&amp; f, Tuple&amp;&amp; t, <span class="built_in">std</span>::index_sequence&lt;I...&gt;)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// This implementation is valid since C++20 (via P1065R2)</span></span><br><span class="line">    <span class="comment">// In C++17, a constexpr counterpart of std::invoke is actually needed here</span></span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">std</span>::invoke(<span class="built_in">std</span>::forward&lt;F&gt;(f), <span class="built_in">std</span>::get&lt;I&gt;(<span class="built_in">std</span>::forward&lt;Tuple&gt;(t))...);</span><br><span class="line">&#125;</span><br><span class="line">&#125;  <span class="comment">// namespace detail</span></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">F</span>, <span class="title">class</span> <span class="title">Tuple</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">constexpr</span> <span class="title">decltype</span>(<span class="title">auto</span>) <span class="title">apply</span>(<span class="title">F</span>&amp;&amp; <span class="title">f</span>, <span class="title">Tuple</span>&amp;&amp; <span class="title">t</span>)</span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">return</span> detail::apply_impl(</span><br><span class="line">        <span class="built_in">std</span>::forward&lt;F&gt;(f), <span class="built_in">std</span>::forward&lt;Tuple&gt;(t),</span><br><span class="line">        <span class="built_in">std</span>::make_index_sequence&lt;<span class="built_in">std</span>::tuple_size_v&lt;<span class="built_in">std</span>::<span class="keyword">remove_reference_t</span>&lt;Tuple&gt;&gt;&gt;&#123;&#125;);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/* tuple traversal in C++17 */</span></span><br><span class="line"><span class="built_in">std</span>::apply([](<span class="keyword">auto</span>&amp;&amp;... item) &#123;</span><br><span class="line">    ((<span class="built_in">std</span>::<span class="built_in">cout</span> &lt;&lt; item &lt;&lt; <span class="string">", "</span>), ...);</span><br><span class="line">    &#125;, t);</span><br></pre></td></tr></table></figure></p>
<h3 id="decltype-auto-C-14"><a href="#decltype-auto-C-14" class="headerlink" title="decltype(auto) (C++14)"></a>decltype(auto) (C++14)</h3><p><code>decltype(auto)</code>十分瞩目，用法<a href="https://stackoverflow.com/questions/24109737/what-are-some-uses-of-decltypeauto" target="_blank" rel="noopener">stackoverflow</a>总结的非常好。这里再简单列一下:</p>
<ul>
<li><p>Return type forwarding in generic code<br>完美转发返回类型</p>
<figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">Fun</span>, <span class="title">class</span>... <span class="title">Args</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">decltype</span>(<span class="title">auto</span>) <span class="title">Example</span>(<span class="title">Fun</span> <span class="title">fun</span>, <span class="title">Args</span>&amp;&amp;... <span class="title">args</span>) </span></span><br><span class="line"><span class="class">&#123;</span> </span><br><span class="line">    <span class="keyword">return</span> fun(<span class="built_in">std</span>::forward&lt;Args&gt;(args)...); </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>Delaying return type deduction in recursive templates<br>在递归模板中延迟返回类型推断</p>
<figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">int</span> i&gt; </span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Int</span> &#123;</span>&#125;;</span><br><span class="line"></span><br><span class="line">constexpr auto iter(Int&lt;0&gt;) -&gt; Int&lt;0&gt;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">int</span> i&gt;</span><br><span class="line">constexpr auto iter(Int&lt;i&gt;) -&gt; decltype(auto) </span><br><span class="line">&#123; <span class="keyword">return</span> iter(Int&lt;i<span class="number">-1</span>&gt;&#123;&#125;); &#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123; <span class="keyword">decltype</span>(iter(Int&lt;<span class="number">10</span>&gt;&#123;&#125;)) a; &#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>一些例子<br><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">int</span> i;</span><br><span class="line"><span class="function"><span class="keyword">int</span>&amp;&amp; <span class="title">f</span><span class="params">()</span></span>;</span><br><span class="line"><span class="keyword">auto</span> x3a = i;                  <span class="comment">// decltype(x3a) is int</span></span><br><span class="line"><span class="keyword">decltype</span>(<span class="keyword">auto</span>) x3d = i;        <span class="comment">// decltype(x3d) is int</span></span><br><span class="line"><span class="keyword">auto</span> x4a = (i);                <span class="comment">// decltype(x4a) is int</span></span><br><span class="line"><span class="keyword">decltype</span>(<span class="keyword">auto</span>) x4d = (i);      <span class="comment">// decltype(x4d) is int&amp;</span></span><br><span class="line"><span class="keyword">auto</span> x5a = f();                <span class="comment">// decltype(x5a) is int</span></span><br><span class="line"><span class="keyword">decltype</span>(<span class="keyword">auto</span>) x5d = f();      <span class="comment">// decltype(x5d) is int&amp;&amp;</span></span><br><span class="line"><span class="keyword">auto</span> x6a = &#123; <span class="number">1</span>, <span class="number">2</span> &#125;;           <span class="comment">// decltype(x6a) is std::initializer_list&lt;int&gt;</span></span><br><span class="line"><span class="keyword">decltype</span>(<span class="keyword">auto</span>) x6d = &#123; <span class="number">1</span>, <span class="number">2</span> &#125;; <span class="comment">// error, &#123; 1, 2 &#125; is not an expression</span></span><br><span class="line"><span class="keyword">auto</span> *x7a = &amp;i;                <span class="comment">// decltype(x7a) is int*</span></span><br><span class="line"><span class="keyword">decltype</span>(<span class="keyword">auto</span>)*x7d = &amp;i;       <span class="comment">// error, declared type is not plain decltype(auto)</span></span><br></pre></td></tr></table></figure></p>
<h3 id="std-invoke-C-17"><a href="#std-invoke-C-17" class="headerlink" title="std::invoke (C++17)"></a>std::invoke (C++17)</h3><p>更广义上的可调用对象的调用，C++20加上了constexpr版本。<br><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="comment">//C++17 ~ C++20</span></span><br><span class="line"><span class="keyword">template</span>&lt; <span class="class"><span class="keyword">class</span> <span class="title">F</span>, <span class="title">class</span>... <span class="title">Args</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">std</span>:</span>:<span class="keyword">invoke_result_t</span>&lt;F, Args...&gt;</span><br><span class="line">invoke(F&amp;&amp; f, Args&amp;&amp;... args) <span class="keyword">noexcept</span>( );</span><br><span class="line"></span><br><span class="line"><span class="comment">//C++20</span></span><br><span class="line"><span class="keyword">template</span>&lt; <span class="class"><span class="keyword">class</span> <span class="title">F</span>, <span class="title">class</span>... <span class="title">Args</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">constexpr</span> <span class="title">std</span>:</span>:<span class="keyword">invoke_result_t</span>&lt;F, Args...&gt;</span><br><span class="line">invoke(F&amp;&amp; f, Args&amp;&amp;... args) <span class="keyword">noexcept</span>( );</span><br></pre></td></tr></table></figure></p>
<p>根据其中<code>F</code>，有<a href="https://en.cppreference.com/w/cpp/utility/functional/invoke" target="_blank" rel="noopener">几种调用的情况</a><br><img src="/2021/05/25/Template-index-sequence-parameter-pack-fold-expression/stdinvoke.jpg" title="std::invoke cases"><br><code>f</code>可以是<code>class T</code>的成员函数指针，成员变量指针(N == 1)。否则<code>f</code>为函数对象，等同于<code>f(t1,t2..)</code>。</p>
<h2 id="Parameter-Pack-C-11"><a href="#Parameter-Pack-C-11" class="headerlink" title="Parameter Pack (C++11)"></a>Parameter Pack (C++11)</h2><h3 id="Syntax"><a href="#Syntax" class="headerlink" title="Syntax"></a>Syntax</h3><p>模板参数包只能作为<strong>模板参数列表</strong>的最后一个参数</p>
<ul>
<li><code>type... Args(optional)</code> 非类型模板参数包</li>
<li><code>typename|class... Args(optional)</code>    类型模板参数包</li>
<li><code>template &lt;parameter-list&gt; typename(C++17)|class ... Args(optional)</code> 模板参数包的模板</li>
<li><code>Args... args(optional)</code> 函数参数包，用于变长参数函数模板的形参声明</li>
<li><code>pattern...</code>    函数包展开：展开为”,”分隔的pattern list</li>
</ul>
<h3 id="Expansion"><a href="#Expansion" class="headerlink" title="Expansion"></a>Expansion</h3><h4 id="函数实参列表"><a href="#函数实参列表" class="headerlink" title="函数实参列表"></a>函数实参列表</h4><p>(注意4, 5),展开可能会出现在函数调用运算符的括号内，在这种情况下，<strong>省略号左侧的最大表达式</strong>或<strong>括号初始列表</strong>是展开的pattern。<br><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line">f(&amp;args...); <span class="comment">// expands to f(&amp;E1, &amp;E2, &amp;E3)</span></span><br><span class="line">f(n, ++args...); <span class="comment">// expands to f(n, ++E1, ++E2, ++E3);</span></span><br><span class="line">f(++args..., n); <span class="comment">// expands to f(++E1, ++E2, ++E3, n);</span></span><br><span class="line">f(<span class="keyword">const_cast</span>&lt;<span class="keyword">const</span> Args*&gt;(&amp;args)...);</span><br><span class="line"><span class="comment">// f(const_cast&lt;const E1*&gt;(&amp;X1), const_cast&lt;const E2*&gt;(&amp;X2), const_cast&lt;const E3*&gt;(&amp;X3))</span></span><br><span class="line">f(h(args...) + args...); <span class="comment">// expands to </span></span><br><span class="line"><span class="comment">// f(h(E1,E2,E3) + E1, h(E1,E2,E3) + E2, h(E1,E2,E3) + E3)</span></span><br></pre></td></tr></table></figure></p>
<h4 id="列表初始化"><a href="#列表初始化" class="headerlink" title="列表初始化"></a>列表初始化</h4><p>(注意sizeof…)<br><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span>... Ts&gt; <span class="function"><span class="keyword">void</span> <span class="title">func</span><span class="params">(Ts... args)</span></span>&#123;</span><br><span class="line">    <span class="keyword">const</span> <span class="keyword">int</span> size = <span class="keyword">sizeof</span>...(args) + <span class="number">2</span>;</span><br><span class="line">    <span class="keyword">int</span> res[size] = &#123;<span class="number">1</span>, args..., <span class="number">2</span>&#125;;</span><br><span class="line">    <span class="comment">// since initializer lists guarantee sequencing, this can be used to</span></span><br><span class="line">    <span class="comment">// call a function on each element of a pack, in order:</span></span><br><span class="line">    <span class="keyword">int</span> dummy[<span class="keyword">sizeof</span>...(Ts)] = &#123; (<span class="built_in">std</span>::<span class="built_in">cout</span> &lt;&lt; args, <span class="number">0</span>)... &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h4 id="函数形参列表"><a href="#函数形参列表" class="headerlink" title="函数形参列表"></a>函数形参列表</h4><p>如果括号在函数声明中出现，函数声明就是<code>pattern</code>。如果两个参数包在同一个<code>pattern</code>出现，则它们同时展开（非笛卡尔积），且参数列表必须长度相等。<br><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span>... Ts, <span class="keyword">int</span>... N&gt; </span><br><span class="line">void g(Ts (&amp;...arr)[N]) &#123;&#125;</span><br><span class="line"><span class="keyword">int</span> n[<span class="number">1</span>];</span><br><span class="line">g&lt;<span class="keyword">const</span> <span class="keyword">char</span>, <span class="keyword">int</span>&gt;(<span class="string">"a"</span>, n); <span class="comment">// Ts (&amp;...arr)[N] expands to </span></span><br><span class="line">                            <span class="comment">// const char (&amp;)[2], int(&amp;)[1]</span></span><br></pre></td></tr></table></figure></p>
<figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span>...&gt; <span class="class"><span class="keyword">struct</span> <span class="title">Tuple</span> &#123;</span>&#125;;</span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> T1, <span class="keyword">typename</span> T2&gt; <span class="class"><span class="keyword">struct</span> <span class="title">Pair</span> &#123;</span>&#125;;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> ...<span class="title">Args1</span>&gt; <span class="title">struct</span> <span class="title">zip</span> &#123;</span></span><br><span class="line">    <span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> ...<span class="title">Args2</span>&gt; <span class="title">struct</span> <span class="title">with</span> &#123;</span></span><br><span class="line">        <span class="keyword">typedef</span> Tuple&lt;Pair&lt;Args1, Args2&gt;...&gt; type;</span><br><span class="line"><span class="comment">//        Pair&lt;Args1, Args2&gt;... is the pack expansion</span></span><br><span class="line"><span class="comment">//        Pair&lt;Args1, Args2&gt; is the pattern</span></span><br><span class="line">    &#125;;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">typedef</span> zip&lt;<span class="keyword">short</span>, <span class="keyword">int</span>&gt;::with&lt;<span class="keyword">unsigned</span> <span class="keyword">short</span>, <span class="keyword">unsigned</span>&gt;::type T1;</span><br><span class="line"><span class="comment">// Pair&lt;Args1, Args2&gt;... expands to</span></span><br><span class="line"><span class="comment">// Pair&lt;short, unsigned short&gt;, Pair&lt;int, unsigned int&gt; </span></span><br><span class="line"><span class="comment">// T1 is Tuple&lt;Pair&lt;short, unsigned short&gt;, Pair&lt;int, unsigned&gt;&gt;</span></span><br></pre></td></tr></table></figure>
<h4 id="模板形参列表"><a href="#模板形参列表" class="headerlink" title="模板形参列表"></a>模板形参列表</h4><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line">展开为非类型模板参数</span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span>... T&gt; <span class="class"><span class="keyword">struct</span> <span class="title">value_holder</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">template</span>&lt;T... Values&gt; <span class="comment">// expands to a non-type template parameter </span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">apply</span> &#123;</span> &#125;;     <span class="comment">// list, such as &lt;int, char, int(&amp;)[5]&gt;</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h4 id="基类列表"><a href="#基类列表" class="headerlink" title="基类列表"></a>基类列表</h4><p>一般意味着构造函数也需要参数包，且有member initializer-list<br><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span>... <span class="title">Mixins</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">class</span> <span class="title">X</span> :</span> <span class="keyword">public</span> Mixins... &#123;</span><br><span class="line"> <span class="keyword">public</span>:</span><br><span class="line">    X(<span class="keyword">const</span> Mixins&amp;... mixins) : Mixins(mixins)... &#123; &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h4 id="lambda捕获"><a href="#lambda捕获" class="headerlink" title="lambda捕获"></a>lambda捕获</h4><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> ...<span class="title">Args</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">void</span> <span class="title">f</span>(<span class="title">Args</span>... <span class="title">args</span>) &#123;</span></span><br><span class="line">    <span class="keyword">auto</span> lm = [&amp;, args...] &#123; <span class="keyword">return</span> g(args...); &#125;;</span><br><span class="line">    lm();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="Attribute-list-属性列表"><a href="#Attribute-list-属性列表" class="headerlink" title="Attribute list 属性列表"></a>Attribute list 属性列表</h4><p>属性列表也允许展开<br><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">void</span> [[attributes...]] function()</span><br></pre></td></tr></table></figure></p>
<h4 id="using"><a href="#using" class="headerlink" title="using"></a>using</h4><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span>... bases&gt;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">X</span> :</span> bases... &#123;</span><br><span class="line">	<span class="keyword">using</span> bases::g...;</span><br><span class="line">&#125;;</span><br><span class="line">X&lt;B, D&gt; x; <span class="comment">// OK: B::g and D::g introduced</span></span><br></pre></td></tr></table></figure>
<h3 id="Fold-Expression-C-17"><a href="#Fold-Expression-C-17" class="headerlink" title="Fold Expression (C++ 17)"></a>Fold Expression (C++ 17)</h3><h3 id="Syntax-1"><a href="#Syntax-1" class="headerlink" title="Syntax"></a>Syntax</h3><ul>
<li><code>pack op ...</code></li>
<li><code>... op pack</code></li>
<li><code>pack op ... op init</code></li>
<li><code>init op ... op pack</code></li>
</ul>
<figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span>... Args&gt;</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">all</span><span class="params">(Args... args)</span> </span>&#123; <span class="keyword">return</span> (... &amp;&amp; args); &#125;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">bool</span> b = all(<span class="literal">true</span>, <span class="literal">true</span>, <span class="literal">true</span>, <span class="literal">false</span>);</span><br><span class="line"> <span class="comment">// within all(), the unary left fold expands as</span></span><br><span class="line"> <span class="comment">//  return ((true &amp;&amp; true) &amp;&amp; true) &amp;&amp; false;</span></span><br><span class="line"> <span class="comment">// b is false</span></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">typename</span> ...Args&gt;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">sum</span><span class="params">(Args&amp;&amp;... args)</span> </span>&#123;</span><br><span class="line"><span class="comment">//    return (args + ... + 1 * 2); // Error: operator with precedence below cast</span></span><br><span class="line">    <span class="keyword">return</span> (args + ... + (<span class="number">1</span> * <span class="number">2</span>)); <span class="comment">// OK</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="CRTP-Curiously-Recurring-Template-Pattern"><a href="#CRTP-Curiously-Recurring-Template-Pattern" class="headerlink" title="CRTP (Curiously Recurring Template Pattern)"></a>CRTP (Curiously Recurring Template Pattern)</h3><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="comment">// CRTP Curiously Recurring Template Pattern</span></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> Child&gt;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Base</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">interface</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">static_cast</span>&lt;Child*&gt;(<span class="keyword">this</span>)-&gt;implementation();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Derived</span> :</span> <span class="keyword">public</span> Base&lt;Derived&gt;</span><br><span class="line">&#123;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">implementation</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="built_in">std</span>::<span class="built_in">cout</span> &lt;&lt; <span class="string">"Derived : implementation"</span>&lt;&lt; <span class="built_in">std</span>::<span class="built_in">endl</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>在基类中定义接口，派生类中实现方法。派生类继承基类的派生类特化，子类回调派生类的实现。用于实现静态多态。</p>
<h4 id="enable-shared-from-this-C-11"><a href="#enable-shared-from-this-C-11" class="headerlink" title="enable_shared_from_this (C++11)"></a>enable_shared_from_this (C++11)</h4><p>需要在类对象的内部中获得一个指向当前对象的<code>shared_ptr</code>对象。<br><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="comment">// std::enable_shared_from_this</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Good</span>:</span> <span class="built_in">std</span>::enable_shared_from_this&lt;Good&gt;</span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">std</span>::<span class="built_in">shared_ptr</span>&lt;Good&gt; getptr() &#123;</span><br><span class="line">        <span class="keyword">return</span> shared_from_this();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">std</span>::<span class="built_in">shared_ptr</span>&lt;Good&gt; gp1 = <span class="built_in">std</span>::make_shared&lt;Good&gt;();</span><br><span class="line">    <span class="built_in">std</span>::<span class="built_in">shared_ptr</span>&lt;Good&gt; gp2 = gp1-&gt;getptr();</span><br><span class="line">    <span class="built_in">std</span>::<span class="built_in">cout</span> &lt;&lt; <span class="string">"gp2.use_count() = "</span> &lt;&lt; gp2.use_count() &lt;&lt; <span class="string">'\n'</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>基本实现<br><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">namespace</span> example</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">D</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">class</span> <span class="title">enable_shared_from_this</span> &#123;</span></span><br><span class="line"><span class="keyword">protected</span>:</span><br><span class="line">    <span class="function"><span class="keyword">constexpr</span> <span class="title">enable_shared_from_this</span><span class="params">()</span> </span>&#123; &#125;</span><br><span class="line">    enable_shared_from_this( enable_shared_from_this <span class="keyword">const</span>&amp;) &#123; &#125;</span><br><span class="line">    enable_shared_from_this&amp; <span class="keyword">operator</span>=(enable_shared_from_this <span class="keyword">const</span>&amp;) &#123;</span><br><span class="line">        <span class="keyword">return</span> *<span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">std</span>::<span class="built_in">shared_ptr</span>&lt;D&gt; shared_from_this() &#123;<span class="keyword">return</span> self_;&#125;</span><br><span class="line">    <span class="built_in">std</span>::<span class="built_in">shared_ptr</span>&lt;D <span class="keyword">const</span>&gt; shared_from_this() <span class="keyword">const</span> &#123; <span class="keyword">return</span> self_;&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="built_in">std</span>::weak_ptr&lt;D&gt; self_;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">friend</span> <span class="built_in">std</span>::<span class="built_in">shared_ptr</span>&lt;D&gt;;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/template/" rel="tag"># template</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2021/04/17/mutex-criticalsection-semaphore/" rel="next" title="mutex, criticalsection, semaphore">
                <i class="fa fa-chevron-left"></i> mutex, criticalsection, semaphore
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2021/06/02/Template-type-traits/" rel="prev" title="[Template] type_traits">
                [Template] type_traits <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
      <div id="sidebar-dimmer"></div>
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/MOB.jpg" alt="HTelepathH">
            
              <p class="site-author-name" itemprop="name">HTelepathH</p>
              <p class="site-description motion-element" itemprop="description">Learning by doing.</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">25</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">12</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">15</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="99088577@qq.com" target="_blank" title="Mail">
                      Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/htelepathh" target="_blank" title="Gitee">
                      Gitee</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/htlpt" target="_blank" title="GitHub">
                      GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/u/6207125886" target="_blank" title="微博">
                      微博</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.zhihu.com/people/huang-he-tu" target="_blank" title="知乎">
                      知乎</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#std-index-sequence-amp-std-tuple"><span class="nav-number">1.</span> <span class="nav-text">std::index_sequence &amp; std::tuple</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#std-apply-C-17"><span class="nav-number">1.1.</span> <span class="nav-text">std::apply (C++17)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#decltype-auto-C-14"><span class="nav-number">1.2.</span> <span class="nav-text">decltype(auto) (C++14)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#std-invoke-C-17"><span class="nav-number">1.3.</span> <span class="nav-text">std::invoke (C++17)</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Parameter-Pack-C-11"><span class="nav-number">2.</span> <span class="nav-text">Parameter Pack (C++11)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Syntax"><span class="nav-number">2.1.</span> <span class="nav-text">Syntax</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Expansion"><span class="nav-number">2.2.</span> <span class="nav-text">Expansion</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#函数实参列表"><span class="nav-number">2.2.1.</span> <span class="nav-text">函数实参列表</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#列表初始化"><span class="nav-number">2.2.2.</span> <span class="nav-text">列表初始化</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#函数形参列表"><span class="nav-number">2.2.3.</span> <span class="nav-text">函数形参列表</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#模板形参列表"><span class="nav-number">2.2.4.</span> <span class="nav-text">模板形参列表</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#基类列表"><span class="nav-number">2.2.5.</span> <span class="nav-text">基类列表</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#lambda捕获"><span class="nav-number">2.2.6.</span> <span class="nav-text">lambda捕获</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Attribute-list-属性列表"><span class="nav-number">2.2.7.</span> <span class="nav-text">Attribute list 属性列表</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#using"><span class="nav-number">2.2.8.</span> <span class="nav-text">using</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Fold-Expression-C-17"><span class="nav-number">2.3.</span> <span class="nav-text">Fold Expression (C++ 17)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Syntax-1"><span class="nav-number">2.4.</span> <span class="nav-text">Syntax</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#CRTP-Curiously-Recurring-Template-Pattern"><span class="nav-number">2.5.</span> <span class="nav-text">CRTP (Curiously Recurring Template Pattern)</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#enable-shared-from-this-C-11"><span class="nav-number">2.5.1.</span> <span class="nav-text">enable_shared_from_this (C++11)</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-angle-double-left"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">HTelepathH</span>

  
</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  
  


  

  

</body>
</html>
